package com.ciban.smarthome.module.ui.base;

import java.util.ArrayList;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.DensityUtils;
import com.ciban.smarthome.common.utils.FileUtils;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.BaseApplication;
import com.ciban.smarthome.module.net.communication.Communicate;
import com.ciban.smarthome.module.ui.container.ListViewDialog;
import com.ciban.smarthome.module.ui.container.ListViewDialog.ListDialogHelper;
import com.ciban.smarthome.module.utils.SystemStatusManager;
import com.lidroid.xutils.BitmapUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class BaseActivity extends Activity {
	protected static final int HANDLER_PING_DISCONN = 100;
	protected static final int HANDLER_PING_CONN = 101;
	protected BaseApplication baseApp;

	protected Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			handMsg(msg);
		};
	};

	protected void handMsg(Message msg) {
		switch (msg.what) {
		case HANDLER_PING_DISCONN:
			displayShort(getResources().getString(R.string.conn_tip));
			break;
		case HANDLER_PING_CONN:

			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTranslucentStatus();
		baseApp = (BaseApplication) this.getApplication();
	}

	protected void setWindowWidth(float bl) {
		int width = DensityUtils.getWindowWidth(this);
		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		p.width = (int) (width * bl); // 宽度设置为屏幕的0.8
		getWindow().setAttributes((android.view.WindowManager.LayoutParams) p);
	}

	/**
	 * 设置状态栏背景状态
	 */
	protected void setTranslucentStatus() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Window win = getWindow();
			WindowManager.LayoutParams winParams = win.getAttributes();
			final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
			winParams.flags |= bits;
			win.setAttributes(winParams);
		}
		SystemStatusManager tintManager = new SystemStatusManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setStatusBarTintResource(0);// 状态栏无背景
	}

	protected void displayShort(String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}


	/**
	 * 展示图片
	 * @param url  uri路径
	 * @param iv
	 */
	protected void displayImg(String url, ImageView iv) {
		if (StringUtils.isEmpty(url))
			return;
		String real_path = ImgUtils.getRealFilePath(this, Uri.parse(url));
		if (FileUtils.isFileExists(real_path)) {
			BitmapUtils utils = new BitmapUtils(this);
			utils.display(iv, real_path);
			utils = null;
		}
	}


	/****************************** 展示更多选项的popwindow **********************************/
	protected PopupWindow menuPopWin;

	protected void showMenuWin(View v) {
		if (menuPopWin == null) {
			View view = View.inflate(this, R.layout.ppw_equip_oprs,
					null);

			TextView tv_equ = (TextView) view
					.findViewById(R.id.oprlist_choose_equ);
			TextView tv_edit = (TextView) view
					.findViewById(R.id.oprlist_edit_equ);
			TextView tv_add = (TextView) view
					.findViewById(R.id.oprlist_add_equ);
			TextView tv_del = (TextView) view
					.findViewById(R.id.oprlist_del_equ);

			tv_equ.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					clickChooseEquipment();
				}
			});

			tv_edit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					clickEditEquipment();
				}
			});
			tv_add.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					clickAddEquipment();
				}
			});
			tv_del.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					clickDelEquipment();
				}
			});

			menuPopWin = new PopupWindow(view, 300, LayoutParams.WRAP_CONTENT);
			menuPopWin.setFocusable(true);
			menuPopWin.setBackgroundDrawable(getResources().getDrawable(
					R.mipmap.group_icon));
			menuPopWin.setOutsideTouchable(true);
		}
		if (!menuPopWin.isShowing())
			menuPopWin.showAsDropDown(v);
	}

	protected void clickChooseEquipment() {
	}

	protected void clickAddEquipment() {
	}

	protected void clickDelEquipment() {
	}

	protected void clickEditEquipment() {
	}

	/************************************************************/
	protected PopupWindow popOprwin;

	protected void showOprWin(View v) {
		if (popOprwin == null) {
			View view = View.inflate(this, R.layout.ppw_mode, null);

			TextView tv_add = (TextView) view.findViewById(R.id.mode_add);
			TextView tv_edit = (TextView) view.findViewById(R.id.mode_edit);

			tv_edit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					oprEdit();
				}
			});
			tv_add.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					oprAdd();
				}
			});

			popOprwin = new PopupWindow(view, 200, LayoutParams.WRAP_CONTENT);
			popOprwin.setFocusable(true);
			popOprwin.setBackgroundDrawable(getResources().getDrawable(
					R.mipmap.group_icon));
			popOprwin.setOutsideTouchable(true);
		}
		if (!popOprwin.isShowing())
			popOprwin.showAsDropDown(v);
	}

	protected void oprEdit() {
	}

	protected void oprAdd() {
	}

	/***********************************************************/
	protected ListViewDialog lvDialog;
	private Communicate conn;

	/**
	 * 展示listview的dialog
	 * @param adapter
	 * @param title
	 * @param list
	 * @param isNew
	 */
	protected void showLvDialog(BaseAdapter adapter, String title,
			ArrayList<String> list, boolean isNew) {

		if (lvDialog == null || isNew) {
			lvDialog = new ListViewDialog(this, R.style.dialog_common);
			lvDialog.setTitle(title);

			if (adapter != null)
				lvDialog.setAdapter(adapter);

			if (list != null)
				lvDialog.setList(list);

			lvDialog.setListener(new ListDialogHelper() {

				@Override
				public void onItemClick(int position) {
					lvItemClick(position);
				}
			});
		}

		lvDialog.show();
	}

	/**
	 * 展示listview的dialog item被选中
	 * @param position
	 */
	protected void lvItemClick(int position) {
	}

	/*************************************************************************/

	public interface alertDialogListener {
		void alertDialogPositive();

		void alertDialogNegative();
	}

	protected void showCommonAlertDialog(String title, String msg,
			final alertDialogListener listener) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(title);
		builder.setMessage(msg);
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				listener.alertDialogNegative();
			}
		});
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				listener.alertDialogPositive();
			}
		});

		builder.create().show();
	}

	/***********************************************************************/
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
